/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.atlas.hive.bridge;


import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.atlas.model.typedef.AtlasTypesDef;
import org.apache.atlas.type.AtlasBuiltInTypes;
import org.apache.atlas.type.AtlasTypeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;


/**
 * Created by sqy on 2018/12/14.
 */
public class RuleDefOperation {
    private static final Logger LOG = LoggerFactory.getLogger(RuleDefOperation.class);

    public static final String RULE_PURPOSE = "rule_purpose";
    public static final String RULE_TASK = "rule_task";

    public static final String PURPOSE_TYPE = "purpose";
    public static final String TASK_TYPE = "task";
    public static final String USEDDATA_TYPE = "usedData";

    public AtlasTypesDef createRuleTypeDef() {
        Set<String> datasetTypes = new HashSet<>();
        datasetTypes.add(AtlasBaseTypeDef.ATLAS_TYPE_DATASET);

        Set<String> processTypes = new HashSet<>();
        processTypes.add(AtlasBaseTypeDef.ATLAS_TYPE_PROCESS);

        AtlasEntityDef purposeDef = AtlasTypeUtil.createClassTypeDef(RULE_PURPOSE,RULE_PURPOSE,"1.0",datasetTypes,
                AtlasTypeUtil.createUniqueRequiredAttrDef("name",new AtlasBuiltInTypes.AtlasStringType()),
                AtlasTypeUtil.createOptionalAttrDef("name",new AtlasBuiltInTypes.AtlasStringType()),
                AtlasTypeUtil.createOptionalAttrDef("description",new AtlasBuiltInTypes.AtlasStringType()));

        AtlasEntityDef taskDef = AtlasTypeUtil.createClassTypeDef(RULE_TASK,RULE_TASK,"1.0",processTypes,
                AtlasTypeUtil.createUniqueRequiredAttrDef("name",new AtlasBuiltInTypes.AtlasStringType()),
                AtlasTypeUtil.createOptionalAttrDef("name",new AtlasBuiltInTypes.AtlasStringType()),
                AtlasTypeUtil.createOptionalAttrDef("description",new AtlasBuiltInTypes.AtlasStringType()

                ));

        AtlasTypesDef ret = AtlasTypeUtil.getTypesDef(Collections.emptyList(),Collections.emptyList(),Collections.emptyList(),
                Arrays.asList(purposeDef,taskDef));

        return ret;
    }

    //舍弃的方法

    public AtlasTypesDef createTypeDefintions() {
        Set<String> datasetTypes = new HashSet<>();
        datasetTypes.add(AtlasBaseTypeDef.ATLAS_TYPE_DATASET);

        Set<String> processTypes = new HashSet<>();
        processTypes.add(AtlasBaseTypeDef.ATLAS_TYPE_PROCESS);

        AtlasEntityDef purposeType = AtlasTypeUtil.createClassTypeDef(PURPOSE_TYPE,PURPOSE_TYPE,"1.0",datasetTypes,
                AtlasTypeUtil.createUniqueRequiredAttrDef("name","string"),
                AtlasTypeUtil.createOptionalAttrDef("name","string"),
                AtlasTypeUtil.createOptionalAttrDef("description","string"),
                AtlasTypeUtil.createOptionalAttrDef("createTime","date"),
                AtlasTypeUtil.createOptionalAttrDef("createBy","string"));

        AtlasEntityDef taskType = AtlasTypeUtil.createClassTypeDef(TASK_TYPE,TASK_TYPE,"1.0",datasetTypes,
                AtlasTypeUtil.createUniqueRequiredAttrDef("name","string"),
                AtlasTypeUtil.createOptionalAttrDef("name","string"),
                AtlasTypeUtil.createOptionalAttrDef("description","string"),
                AtlasTypeUtil.createOptionalAttrDef("createTime","date"),
                AtlasTypeUtil.createOptionalAttrDef("createBy","string"));


        AtlasEntityDef usedDataType = AtlasTypeUtil.createClassTypeDef(USEDDATA_TYPE,USEDDATA_TYPE,"1.0",processTypes,
                AtlasTypeUtil.createUniqueRequiredAttrDef("name","string"),
                AtlasTypeUtil.createListRequiredAttrDef("inputs",AtlasBaseTypeDef.getArrayTypeName(PURPOSE_TYPE)),
                AtlasTypeUtil.createListRequiredAttrDef("outputs",AtlasBaseTypeDef.getArrayTypeName(TASK_TYPE)),
                AtlasTypeUtil.createOptionalAttrDef("name","string"),
                AtlasTypeUtil.createOptionalAttrDef("createTime","date"),
                AtlasTypeUtil.createOptionalAttrDef("description","string"),
                AtlasTypeUtil.createOptionalAttrDef("createBy","string"));


        AtlasTypesDef ret = AtlasTypeUtil.getTypesDef(Collections.emptyList(),Collections.emptyList(),Collections.emptyList(),
                Arrays.asList(purposeType,usedDataType,taskType));

        return ret;
    }




}
